I have to build a condition based on one or two values.
const getLabel = (type?: string, typeXy?: string) => {
let label;
switch (type || typeXy) {
case 'Value A':
label = 'Label A';
break;
case 'Value B':
label = 'Label B';
break;
case 'Value C':
case 'Value X':
label = 'Label C + X';
break;
case 'Value Y':
label = 'Label C + Y';
break;
default:
break;
}
return label;
};
I need a dubbel case on two places. When I add case 'Value C': above case 'Value Y': I get:
Duplicate case label.eslintno-duplicate-case
1) I add // eslint-disable-next-line no-duplicate-case but my code isn't working yet. Or is there a cleaner way anyway?
2) I can change my switch from the or to the and: (type || typeXy) but then the first case isn't working? How do I support both or and and?
How about using an object literal as lookup table where one can precisely express with, which result for which case one does prefer ?..
const getLabel = (type/*?: string*/, typeXy/*?: string*/) => {
return ({
'Value A': 'Label A',
'Value B': 'Label B',
'Value C': 'Label C',
'Value X': 'Label X',
'Value Y': 'Label Y',
'Value CValue X': 'Label C + X',
'Value XValue C': 'Label C + X',
'Value CValue Y': 'Label C + Y',
'Value YValue C': 'Label C + Y',
})[(type ?? '') + (typeXy ?? '')];
};
console.log("getLabel('Value A') ...", getLabel('Value A'));
console.log("getLabel('Value B', null) ...", getLabel('Value B', null));
console.log("getLabel(null, 'Value C') ...", getLabel(null, 'Value C'));
console.log("getLabel(undefined, 'Value X') ...", getLabel(undefined, 'Value X'));
console.log("getLabel('Value Y') ...", getLabel('Value Y'));
console.log("getLabel('Value C', 'Value X') ...", getLabel('Value C', 'Value X'));
console.log("getLabel('Value X', 'Value C') ...", getLabel('Value X', 'Value C'));
console.log("getLabel('Value C', 'Value Y') ...", getLabel('Value C', 'Value Y'));
console.log("getLabel('Value Y', 'Value C') ...", getLabel('Value Y', 'Value C'));
.as-console-wrapper { min-height: 100%!important; top: 0; }
If I understand your question you want to conditionally check if both arguments are passed and if so, use a logical AND comparison, otherwise if only one or the other is passed revert to a logical OR comparison.
Use an outer switch to branch on the logical AND/OR condition, and then use nested switches to evaluate the specific AND or OR combinations.
const getLabel = (type /*?: string*/, typeXy /*?: string*/) => {
switch (true) {
case !!(type && typeXy):
switch (type) {
case "Value C": {
let label = "Label C";
switch (typeXy) {
case "Value X":
return label + " + X";
case "Value Y":
return label + " + Y";
}
}
}
break;
case !!(type || typeXy):
switch (type || typeXy) {
case "Value A":
return "Label A";
case "Value B":
return "Label B";
}
}
};
console.log(getLabel("Value A")); // Label A
console.log(getLabel("Value B")); // Label B
console.log(getLabel(undefined, "Value A")); // Label A
console.log(getLabel(undefined, "Value B")); // Label B
console.log(getLabel("Value C", "Value X")); // Label C + X
console.log(getLabel("Value C", "Value Y")); // Label C + Y